import { request } from "@/utils/java.js";

/**
 * 竞赛相关API接口
 */

// 获取竞赛主页数据（包含报名数据、风采展示、参赛情况、当前赛段等）
export const getCompetitionHome = () => {
	return request({
		method: 'POST',
		data: {},
		url: '/appcompetition/home'
	}).then(res => {
        console.log('[API] getCompetitionHome', res);
        return res;
	});
};

// 获取竞赛详情
export const getCompetitionDetail = (id) => {
	return request({
		method: 'POST',
		data: { id },
		url: '/appcompetition/detail'
	});
};

// 获取竞赛资讯列表
export const getCompetitionInfoList = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/info'
	});
};

// 搜索竞赛
export const searchCompetition = (keyword) => {
	return request({
		method: 'POST',
		data: { keyword },
		url: '/appcompetition/search'
	});
};

// 获取竞赛资讯详情
export const getCompetitionInfoDetail = (id) => {
	return request({
		method: 'POST',
		data: { id },
		url: '/appcompetition/infoDetail'
	});
};

// 获取竞赛通知列表
export const getNotificationList = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { type: 0, matchId: 56 }
		url: '/appcompetition/notificationList'
	});
};

// 获取通知详情
export const getNotificationDetail = (id) => {
	return request({
		method: 'POST',
		data: { id },
		url: '/appcompetition/notificationDetail'
	});
};

// 获取竞赛入口数据
export const getEntrance = (competitionId, userId) => {
	return request({
		method: 'POST',
		data: { 
			competition_id: competitionId,
			user_id: userId
		},
		url: '/appcompetition/entrance'
	});
};

// 执行签到
export const checkin = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { user_id, match_id }
		url: '/appcompetition/checkin'
	});
};

// 获取签到历史
export const getCheckinHistory = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { user_id, match_id, limit }
		url: '/appcompetition/checkinHistory'
	});
};

// 获取签到统计信息
export const getCheckinStats = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { user_id, match_id }
		url: '/appcompetition/checkinStats'
	});
};

// 重置今日签到（仅用于开发测试）
export const resetTodayCheckin = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { user_id, match_id }
		url: '/appcompetition/resetTodayCheckin'
	});
};

// 获取积分明细（包含所有场景的积分记录）
export const getPointDetail = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { user_id, match_id, limit }
		url: '/appcompetition/pointDetail'
	});
};

// 获取个人竞技题目列表
export const getPKQuestions = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { examId, count }
		url: '/appcompetition/pk/questions'
	});
};

// 提交个人竞技答题结果
export const submitPKResult = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { userId, matchId, answers }
		url: '/appcompetition/pk/submit'
	});
};

// 获取组队竞技规则信息
export const getTeamPKInfo = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { matchId }
		url: '/appcompetition/teampk/info'
	});
};

// ==================== 答题闯关相关API ====================
// 注：后端使用 adventure 命名（对应数据库表 match_adventure_*）

// 获取答题闯关信息（关卡列表、规则、积分等）- 对应页面：pk/levels.vue
export const getAdventureInfo = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { matchId, userId }
		url: '/appcompetition/adventure/info'
	});
};

// 获取答题闯关题目列表 - 对应页面：pk/question.vue
export const getAdventureQuestions = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { matchId, userId, checkpointId }
		url: '/appcompetition/adventure/questions'
	});
};

// 提交答题闯关答案 - 对应页面：pk/question.vue
export const submitAdventureAnswers = (data = {}, userId, matchId) => {
	if (!userId || !matchId) {
		throw new Error('userId 和 matchId 是必填参数');
	}
	return request({
		method: 'POST',
		data: data,  // body参数：checkpointId, answers
		url: '/appcompetition/adventure/submit',
		params: {  // URL查询参数
			userId: userId,
			matchId: matchId
		}
	});
};

// 获取答题闯关历史记录 - 对应页面：pk/record.vue
export const getAdventureHistory = (params = {}) => {
	return request({
		method: 'POST',
		data: params,  // { matchId, userId }
		url: '/appcompetition/adventure/history'
	});
};

// ==================== 大冒险相关API ====================
// 注：大冒险与答题闯关共用后端API和题库，通过 type=1 区分

// 获取大冒险信息（关卡列表、规则、积分等）- 对应页面：adventure/adventure-levels.vue
export const getBigAdventureInfo = (params = {}) => {
	return request({
		method: 'POST',
		data: { ...params, type: 1 },  // type=1 表示大冒险
		url: '/appcompetition/adventure/info'
	});
};

// 获取大冒险题目列表 - 对应页面：adventure/adventure-question.vue
export const getBigAdventureQuestions = (params = {}) => {
	return request({
		method: 'POST',
		data: { ...params, type: 1 },  // type=1 表示大冒险
		url: '/appcompetition/adventure/questions'
	});
};

// 提交大冒险答案 - 对应页面：adventure/adventure-question.vue
export const submitBigAdventureAnswers = (data = {}, userId, matchId) => {
	if (!userId || !matchId) {
		throw new Error('userId 和 matchId 是必填参数');
	}
	return request({
		method: 'POST',
		data: data,  // body参数：checkpointId, answers
		url: '/appcompetition/adventure/submit',
		params: {  // URL查询参数
			userId: userId,
			matchId: matchId,
			type: 1  // type=1 表示大冒险
		}
	});
};

// 获取大冒险历史记录 - 对应页面：adventure/adventure-record.vue
export const getBigAdventureHistory = (params = {}) => {
	return request({
		method: 'POST',
		data: { ...params, type: 1 },  // type=1 表示大冒险
		url: '/appcompetition/adventure/history'
	});
};

// ==================== 阅读课件相关API ====================

// 获取课件列表 - 对应页面：learning/courseware-list.vue
export const getCoursewareList = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/courseware/list'
	});
};

// 获取课件详情 - 对应页面：learning/courseware-detail.vue
export const getCoursewareDetail = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/courseware/detail'
	});
};

// 记录课件学习时长 - 对应页面：learning/courseware-detail.vue
export const recordCoursewareStudy = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/courseware/record'
	});
};

// ==================== 错题学习相关API ====================

// 获取用户错题列表 - 对应页面：learning/wrong-questions.vue
export const getWrongQuestions = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/wrongquestions/list'
	});
};

// ==================== 碎片化学习相关API ====================

// 获取碎片化学习题目列表 - 对应页面：learning/fragment-learning.vue
export const getFragmentQuestions = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/fragment/questions'
	});
};

// ==================== 模拟考试相关API ====================

// 获取模拟考试题目列表 - 对应页面：learning/mock-exam.vue
export const getMockExamQuestions = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/mockexam/questions'
	});
};

// 提交模拟考试答案 - 对应页面：learning/mock-exam.vue
export const submitMockExam = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/mockexam/submit'
	});
};

// ==================== 社区问答相关API ====================

// 获取社区问答列表 - 对应页面：community/community-qa.vue
export const getQAList = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/qa/list'
	});
};

// 获取问答详情（包含回复列表） - 对应页面：community/community-qa.vue
export const getQADetail = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/qa/detail'
	});
};

// 提交问答回复 - 对应页面：community/qa-submit.vue
export const submitQAReply = (params = {}) => {
	return request({
		method: 'POST',
		data: params,
		url: '/appcompetition/qa/submit'
	});
};

// ==================== 竞赛报名相关API ====================

/**
 * 检查用户是否已报名某竞赛
 * @param {Number} userId - 用户ID
 * @param {Number} matchId - 竞赛ID
 */
export const checkRegistration = (userId, matchId) => {
	return request({
		method: 'POST',
		data: { userId, matchId },
		url: '/appcompetition/checkRegistration'
	});
};

/**
 * 获取企业列表（用于报名表单选择）
 */
export const getCompanyList = () => {
	return request({
		method: 'POST',
		data: {},
		url: '/appcompetition/company/list'
	});
};

/**
 * 提交竞赛报名
 * @param {Object} data - 报名数据
 */
export const submitRegistration = (data) => {
	return request({
		method: 'POST',
		data,
		url: '/appcompetition/register'
	});
};